package com.czl.base.util

import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.linphone.imp.InitCallPhoneImp

object TestHelper {

    private var isTest = false
    private var ob: Observable<String>? = null

    fun test() {
        if (!isTest) return
        ob!!.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()).subscribe(object : Observer<String> {
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(s: String) {
                    if ("1" == s) {
                        InitCallPhoneImp.getIntent().answer()
                    } else if ("2" == s) {
                        InitCallPhoneImp.getIntent().decline()
                    }
                }

                override fun onError(e: Throwable) {

                }

                override fun onComplete() {

                }

            })
    }

    fun start(callTime: Long, hangTime: Long) {
        isTest = true
        if (callTime == 0L) {
            ob = Observable.create { e ->
                try {
                    Thread.sleep(hangTime * 1000) // 假设此处是耗时操作
                } catch (ee: Exception) {
                    ee.printStackTrace()
                }
                e.onNext("2")
                e.onComplete()
            }
        } else {
            var o1 = Observable.create<String> { e ->
                try {
                    Thread.sleep(callTime * 1000) // 假设此处是耗时操作
                } catch (ee: Exception) {
                    ee.printStackTrace()
                }
                e.onNext("1")
                e.onComplete()
            }
            var o2 = Observable.create<String> { e ->
                try {
                    Thread.sleep(hangTime * 1000) // 假设此处是耗时操作
                } catch (ee: Exception) {
                    ee.printStackTrace()
                }
                e.onNext("2")
                e.onComplete()
            }
            ob = Observable.concat(o1, o2)
        }
    }

    fun stop() {
        isTest = false
    }

}